package com.yazi.mxz.common.service.impl;


import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyEntityNotExistsException;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.exception.MyNoPermissionException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.db.dao.ArticleDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Article;
import com.yazi.mxz.common.service.ArticleService;

@Service("articleService")
public class ArticleServiceImpl extends BasicService<Article> implements ArticleService {
	private static Logger log = LoggerFactory.getLogger(ArticleServiceImpl.class);
	
	@Resource
	ArticleDao articleDao;

	@Override
	protected Dao<Article> getDao() {
		return articleDao;
	}
	
	@Override
	public Article addOrEdit(Article article) throws MyException {
		Long articleId = article.getArticleId();
		if (null != articleId && articleId > 0) {
			log.debug("addOrEdit->edit：articleId={}", articleId);
			Article destArticle = articleDao.get(articleId);
			if (null == destArticle) {
				throw new MyEntityNotExistsException();
			}
			destArticle = ValueFactory.combine(article, destArticle);
			destArticle.setUpdateTime(DateUtil.getTimeStamp());
			return articleDao.merge(destArticle);
		} else {
			log.debug("addOrEdit->add：title={}", article.getTitle());
			article = ValueFactory.validate(article);
			article.setAddTime(DateUtil.getTimeStamp());
			article.setUpdateTime(DateUtil.getTimeStamp());
			article.setArticleId(null);
			return articleDao.persist(article);
		}
	}

	@Override
	public Page<Article> listByRootCatId(long catId, Searching si) {
		log.debug("listByRootCatId：catId={}", catId);
		return articleDao.listByRootCatId(catId, si);
	}

	@Override
	public void removeByStoreId(long storeId, long[] articleIds) {
		log.debug("removeByStoreId：storeId={},articleIds={}", storeId, articleIds);
		articleDao.deleteByStoreId(storeId, articleIds);
	}

	@Override
	public Article addOrEditByStoreId(long storeId, Article article) throws MyException {
		Long articleId = article.getArticleId();
		if (null != articleId && articleId > 0) {
			
			log.debug("addOrEditByStoreId->edit：articleId={}", articleId);
			Article destArticle = articleDao.get(articleId);
			if (storeId != destArticle.getStoreId()) {
				throw new MyNoPermissionException();
			}
			destArticle = ValueFactory.combine(article, destArticle);
			destArticle.setUpdateTime(DateUtil.getTimeStamp());
			return articleDao.merge(destArticle);
			
		} else {
			
			log.debug("addOrEditByStoreId->add：title={}", article.getTitle());
			article = ValueFactory.validate(article);
			article.setStoreId(storeId);
			article.setAddTime(DateUtil.getTimeStamp());
			article.setUpdateTime(DateUtil.getTimeStamp());
			article.setArticleId(null);
			return articleDao.persist(article);
			
		}
	}

}
